package net.fdukedom.epicurus.domain.util;

import java.util.List;

import net.fdukedom.epicurus.domain.entity.Group;
import net.fdukedom.epicurus.tools.logging.Logger;
import net.fdukedom.epicurus.tools.logging.WriterId;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.criterion.Expression;

public class GroupHelper {
    /**
     * The only instance of the class.
     */
    private static final GroupHelper instance = new GroupHelper();

    private Logger logger = Logger.getInstance(WriterId.DOMAIN);

    /**
     * Default constructor.
     */
    private GroupHelper() {
        //this is an utility class only one instance is allowed
    }
    
    /**
     * Returns the only instance of the class.
     *
     * @return the only instance of the class
     */
    public static GroupHelper getInstance() {
        return instance;
    }

    /**
     * Finds group in database by name.
     *
     * @param name group's name
     *
     * @return group with corresponding name or null if no group was found
     */
    public Group findByName(String name) {
        Session session = HibernateUtil.getSession();
        Criteria criteria = session.createCriteria(Group.class);
        List users = criteria.add(Expression.like("name", name)).list();
        if (users.size() > 0) {
            return (Group) users.get(0);
        }
        logger.logInfo("Failed group lookup. Group name " + name);
        return null;
    }
}
